
package com.kugou.game.sdk.protocol;

import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.framework.utils.UrlEncodeUtil;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.utils.ProtocolCmd;
import com.kugou.game.sdk.utils.RSAUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 描述:绑定手机协议类
 *
 * @author jerryliu
 * @since 2015年7月24日 下午6:05:40
 */
public class BindPhoneTask {
    private ArrayList<String> mCmdList = new ArrayList<String>();

    public BindPhoneResponse request(String userName, String mobilePhone, String smsCheckCode,
            String password) {
        ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();
        HttpBaseRequestPackage req = new HttpBaseRequestPackage();
        req.setUserName(userName);
        BindPhoneResponse respObj = new BindPhoneResponse();
        ResponsePackage resp = new BindPhoneResponsePackage();
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("cmd", ProtocolCmd.CMD_BIND_PHONE_NEW);
        params.put("username", userName);
        params.put("mobile", mobilePhone);
        params.put("checkcode", smsCheckCode);
        params.put("password", RSAUtil.getRASEncrypt(UrlEncodeUtil.encode(password, "utf-8")));
        requestParams.add(params);
        req.setRequestParams(requestParams);
        mCmdList.add(ProtocolCmd.CMD_BIND_PHONE_NEW);
        try {
            KGHttpClient.request(req, resp);
            resp.getResponseData(respObj);
            return respObj;
        } catch (Exception e) {
        }
        return null;
    }

    class BindPhoneResponsePackage extends HttpBaseResponsePackage<BindPhoneResponse> {
        @Override
        protected void handleResponse(BindPhoneResponse response, String responseJsonStr) {
            try {
                LogUtil.d("response", "BindPhoneTask:" + responseJsonStr);
                JSONArray responseArray = new JSONArray(responseJsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {
                    if (i >= mCmdList.size()) {
                        break;
                    }
                    JSONObject jsonResp = responseArray.optJSONObject(i);
                    String cmd = jsonResp.optString("cmd");
                    String code = jsonResp.optString("code");
                    String message = jsonResp.optString("message");
                    String prompt = jsonResp.optString("prompt");
                    response.setCode(code);
                    response.setMessage(message);
                    response.setPrompt(prompt);
                    if (jsonResp.optInt("code") != 1) {
                        continue;
                    }
                    if (jsonResp.isNull("data")) {
                        continue;
                    }
                    // 根据cmd解析每个请求
                    if (ProtocolCmd.CMD_BIND_PHONE_NEW.equalsIgnoreCase(cmd)) {
                        JSONObject jsonResult = jsonResp.optJSONObject("data");
                        if (jsonResult != null) {
                            response.setSecurityScore(jsonResult.optInt("safety_score"));
                        }
                    }
                    response.setOk(true);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

    }

    public static class BindPhoneResponse extends ResponseMessage {

        private String rcode;

        private String rmessage_cn;

        private String rprompt;

        private int securityScore;

        public String getRcode() {
            return rcode;
        }

        public void setRcode(String rcode) {
            this.rcode = rcode;
        }

        public String getRmessage_cn() {
            return rmessage_cn;
        }

        public void setRmessage_cn(String rmessage_cn) {
            this.rmessage_cn = rmessage_cn;
        }

        public String getRprompt() {
            return rprompt;
        }

        public void setRprompt(String rprompt) {
            this.rprompt = rprompt;
        }

        public int getSecurityScore() {
            return securityScore;
        }

        public void setSecurityScore(int securityScore) {
            this.securityScore = securityScore;
        }
    }
}
